package com.spic.business.train.autonomic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spic.business.common.datascope.anotation.BusinessDataScope;
import com.spic.business.train.autonomic.domain.TrainAutonomicLearning;
import com.spic.business.train.autonomic.mapper.TrainAutonomicLearningMapper;
import com.spic.business.train.autonomic.service.ITrainAutonomicLearningService;
import com.spic.business.train.trainInfo.service.ITrainInfoService;
import com.spic.business.train.trainUser.domain.TrainUser;
import com.spic.business.train.trainUser.service.ITrainUserService;
import com.spic.common.core.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 课件自主学习Service业务层处理
 *
 * @author spic
 * @date 2021-10-21
 */
@Service
public class TrainAutonomicLearningServiceImpl extends ServiceImpl<TrainAutonomicLearningMapper, TrainAutonomicLearning> implements ITrainAutonomicLearningService
{
    @Autowired
    private TrainAutonomicLearningMapper trainAutonomicLearningMapper;

    @Autowired
    private ITrainUserService trainUserService;

    @Autowired
    private ITrainInfoService trainInfoService;

    /**
     * 查询课件自主学习
     *
     * @param id 课件自主学习主键
     * @return 课件自主学习
     */
    @Override
    public TrainAutonomicLearning selectTrainAutonomicLearningById(Long id)
    {
        return trainAutonomicLearningMapper.selectTrainAutonomicLearningById(id);
    }

    /**
     * 查询课件自主学习列表
     *
     * @param trainAutonomicLearning 课件自主学习
     * @return 课件自主学习
     */
    @Override
    @BusinessDataScope(menuPath = "business/train/createtest/index")
    public List<TrainAutonomicLearning> selectTrainAutonomicLearningList(TrainAutonomicLearning trainAutonomicLearning)
    {
        trainAutonomicLearning.setUserId(SecurityUtils.getUserId()+"");
        return trainAutonomicLearningMapper.selectTrainAutonomicLearningList(trainAutonomicLearning);
    }

    /**
     * 新增课件自主学习
     *
     * @param trainAutonomicLearning 课件自主学习
     * @return 结果
     */
    @Override
    public int insert(TrainAutonomicLearning trainAutonomicLearning)
    {
        return trainAutonomicLearningMapper.insert(trainAutonomicLearning);
    }

    @Override
    public boolean batchInsert(List<TrainAutonomicLearning> trainAutonomicLearning) {
        return saveBatch(trainAutonomicLearning);
    }

    /**
     * 修改课件自主学习
     *
     * @param learning 课件自主学习
     * @return 结果
     */
    @Override
    public int updateProgress(TrainAutonomicLearning learning)
    {
        int row = trainAutonomicLearningMapper.updateTrainAutonomicLearning(learning);
        if(learning.isFinished()){
            int progress = calculateUserProgress(learning.getTrainUserId());
            //回写用户培训进度
            trainUserService.updateTrainUser(TrainUser.updateProcess(learning.getTrainUserId(), progress));
            TrainUser trainUser = trainUserService.selectTrainUserById(learning.getTrainUserId());
            trainInfoService.updateTrainInfoforUserTrainEnd(trainUser.getTrainInfoId());
        }
        return row;
    }

    /**
     * 计算用户培训总学习进度
     * @param trainUserId
     * @return
     */
    private int calculateUserProgress(Long trainUserId) {
        int total = super.count(
                new LambdaQueryWrapper<TrainAutonomicLearning>()
                .eq(TrainAutonomicLearning::getTrainUserId, trainUserId)
        );
        int finished = super.count(
                new LambdaQueryWrapper<TrainAutonomicLearning>()
                        .eq(TrainAutonomicLearning::getTrainUserId, trainUserId)
                        .eq(TrainAutonomicLearning::getSchedule, 1)
        );
        return (int)((double)finished / total * 100);
    }

    /**
     * 删除课件自主学习信息
     *
     * @param id 课件自主学习主键
     * @return 结果
     */
    @Override
    public int deleteTrainAutonomicLearningById(Long id)
    {
        return trainAutonomicLearningMapper.deleteTrainAutonomicLearningById(id);
    }

    /**
     * 删除课件自主学习信息
     *
     * @param trainUserIds 课件自主学习主键
     * @return 结果
     */
    @Override
    public int deleteTrainAutonomicLearningByTrainUserIds(List<Long> trainUserIds)
    {
        if(CollectionUtils.isEmpty(trainUserIds)) return 0;
        return trainAutonomicLearningMapper.deleteTrainAutonomicLearningByIds(trainUserIds);
    }
}
